package com.hefeng.example.controller;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.druid.util.MapComparator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Ordering;
import com.hefeng.example.controller.entiy.ParamEntity;
import com.hefeng.example.domain.CustomerManualWorkOrder;
import com.hefeng.example.service.CustomerManualWorkOrderService;
import com.hefeng.example.service.SichuangResultService;
import com.hefeng.example.service.WorkUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.hefeng.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/ManualWork")
@Api(tags = "工单接口")
public class CustomerManualWorkOrderController {


    @Autowired
    private WorkUserService workUserService;

    @Autowired
    private CustomerManualWorkOrderService customerManualWorkOrderService;

    @Autowired
    private SichuangResultService sichuangResultService;


    /**
     * 转派工单列表
     *
     * @param
     * @return
     */
    @PostMapping(value = "/dispatcherWork", produces = "application/json;charset=UTF-8")
    public AjaxResult slelct(@RequestBody ParamEntity param) {

        Map<String, Object> data = new HashMap<String, Object>();
        Page<CustomerManualWorkOrder> page = new Page<>();
        IPage gasList = workUserService.getList(param.getUserId(), param.getPage(), param.getPageSize());
        data.put("gasList", gasList);
        data.size();
        return AjaxResult.success(gasList);
    }

    /**
     * 处理中工单列表
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/slelctdisposeingWork", produces = "application/json;charset=UTF-8")
    public AjaxResult disposeing(@RequestBody ParamEntity param) {
        String userType = param.getUserType();
        //判断用户类型 01维保  02四川合丰
        switch (userType) {
            case "01":

                Page page = new Page(param.getPage(), param.getPageSize());
                List<Map> list = workUserService.disposeingWork(param.getUserId());
                List<Map> list1 = workUserService.disposeingManualWork(param.getUserId());
                //合并集合
                List<Map> collect = Stream.of(list, list1)
                        .flatMap(Collection::stream)
                        .distinct()
                        .collect(Collectors.toList());

                final List<Map> beanList = ListUtil.toList(collect);
                final List<Map> order = ListUtil.sortByProperty(beanList, "CREATED_TIME");

                int total = order.size();
                page.setTotal(total);
                int startIndex = (param.getPage() - 1) * param.getPageSize();
                int endIndex = Math.min(startIndex + param.getPageSize(), total);
                if (endIndex >= total) {
                    endIndex = total;
                }
                page.addAll(order.subList(startIndex, endIndex));
                PageInfo pageInfo = new PageInfo(page);
                pageInfo.setPageSize(param.getPageSize());
                pageInfo.getSize();
                return AjaxResult.success(pageInfo);

            case "02":
                Page page2 = new Page(param.getPage(), param.getPageSize());
                List<Map> list02 = sichuangResultService.disposeingAlarmWork(param.getUserId());
                int total02 = list02.size();
                page2.setTotal(total02);
                int startIndex02 = (param.getPage() - 1) * param.getPageSize();
                int endIndex02 = Math.min(startIndex02 + param.getPageSize(), total02);
                if (endIndex02 >= total02) {
                    endIndex = total02;
                }
                page2.addAll(list02.subList(startIndex02, endIndex02));
                PageInfo pageInfo02 = new PageInfo(page2);
                pageInfo02.setPageSize(param.getPageSize());
                pageInfo02.getSize();
                return AjaxResult.success(pageInfo02);
        }

        return AjaxResult.success("查询成功");
    }

    /**
     * 逾期工单列表
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/LateWork", produces = "application/json;charset=UTF-8")
    public AjaxResult LateWork(@RequestBody ParamEntity param) {


        String userType = param.getUserType();
        //判断用户类型 01维保  02四川合丰
        switch (userType) {
            case "01":
                Page page = new Page(param.getPage(), param.getPageSize());
                List<Map> list2 = workUserService.LateWork(param.getUserId());
                List<Map> list3 = workUserService.LateWorkT(param.getUserId());

                //合并集合
                List<Map> collect1 = Stream.of(list2, list3)
                        .flatMap(Collection::stream)
                        .distinct()
                        .collect(Collectors.toList());


                final List<Map> beanList = ListUtil.toList(collect1);
                final List<Map> order = ListUtil.sortByProperty(beanList, "CREATED_TIME");

                //分页
                int total = order.size();
                page.setTotal(total);
                int startIndex = (param.getPage() - 1) * param.getPageSize();
                int endIndex = Math.min(startIndex + param.getPageSize(), total);
                if (endIndex >= total) {
                    endIndex = total;
                }

                page.addAll(order.subList(startIndex, endIndex));
                PageInfo pageInfo = new PageInfo(page);
                pageInfo.setPageSize(param.getPageSize());
                pageInfo.getSize();


                return AjaxResult.success(pageInfo);
            case "02":
                Page page2 = new Page(param.getPage(), param.getPageSize());
                List<Map> list02 = sichuangResultService.LateWork(param.getUserId());
                int total02 = list02.size();
                page2.setTotal(total02);
                int startIndex02 = (param.getPage() - 1) * param.getPageSize();
                int endIndex02 = Math.min(startIndex02 + param.getPageSize(), total02);
                if (endIndex02 >= total02) {
                    endIndex = total02;
                }
                page2.addAll(list02.subList(startIndex02, endIndex02));
                PageInfo pageInfo02 = new PageInfo(page2);
                pageInfo02.setPageSize(param.getPageSize());
                pageInfo02.getSize();
                return AjaxResult.success(pageInfo02);
        }

        return AjaxResult.success("查询成功");
    }


    /**
     * 维保人员已完成工单列表
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/finishWork", produces = "application/json;charset=UTF-8")
    public AjaxResult finishWork(@RequestBody ParamEntity param) {
        String userType = param.getUserType();
        //判断用户类型 01维保  02四川合丰
        switch (userType) {
            case "01":
                Page page = new Page(param.getPage(), param.getPageSize());
                List<Map> list2 = workUserService.finishWork(param.getUserId());
                List<Map> list3 = workUserService.finishWorkT(param.getUserId());

                //合并集合
                 List<Map> collect1 = Stream.of(list2, list3)
                        .flatMap(Collection::stream)
                        .distinct()
                        .collect(Collectors.toList());

               final List<Map> beanList = ListUtil.toList(collect1);
               final List<Map> order = ListUtil.sortByProperty(beanList, "CREATED_TIME");
                Collections.reverse(order);



                int total = order.size();
                page.setTotal(total);
                int startIndex = (param.getPage() - 1) * param.getPageSize();
                int endIndex = Math.min(startIndex + param.getPageSize(), total);
                if (endIndex >= total) {
                    endIndex = total;
                }
                page.addAll(order.subList(startIndex, endIndex));
                PageInfo pageInfo = new PageInfo(page);
                pageInfo.setPageSize(param.getPageSize());
                pageInfo.getSize();
                return AjaxResult.success(pageInfo);
            case "02":

                Page page2 = new Page(param.getPage(), param.getPageSize());
                List<Map> list02 = sichuangResultService.finishWork(param.getUserId());
                int total02 = list02.size();
                page2.setTotal(total02);
                int startIndex02 = (param.getPage() - 1) * param.getPageSize();
                int endIndex02 = Math.min(startIndex02 + param.getPageSize(), total02);
                if (endIndex02 >= total02) {
                    endIndex = total02;
                }
                page2.addAll(list02.subList(startIndex02, endIndex02));
                PageInfo pageInfo02 = new PageInfo(page2);
                pageInfo02.setPageSize(param.getPageSize());
                pageInfo02.getSize();
                return AjaxResult.success(pageInfo02);

        }
        return AjaxResult.success("查询成功");
    }

    @PostMapping("/getDeviceList")
    public AjaxResult getDeviceList(CustomerManualWorkOrder customerManualWorkOrder, long current, long size) {
        Map<String, Object> data = new HashMap<String, Object>();
        Page<CustomerManualWorkOrder> page = new Page<>();
        IPage gasList = customerManualWorkOrderService.getDeviceList(customerManualWorkOrder, current, size);
        data.put("gasList", gasList);
        return AjaxResult.success(gasList);
    }
}
